//
//  ImageDropperView.m
//  Words2
//
//  Created by Mathieu on 14/10/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "ImageDropperView.h"
#import "PhotoSearchController.h"

@implementation ImageDropperView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		// register for drag and drop
		[self registerForDraggedTypes:
			[NSArray arrayWithObjects:NSURLPboardType, nil]];
    }
    return self;
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	NSRect bounds = [self bounds];
	[[NSColor blueColor] set];
	[NSBezierPath fillRect:bounds];
	
	//draw white background if highlighted
	if(highlighted){
		[[NSColor whiteColor] set];
	}else{
		[bgColor set];
	}
}




//Dragging
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
	
	if([sender draggingSource] != self){
		NSPasteboard *pb = [sender draggingPasteboard];
		//		NSLog(@"Types: %@", [pb types]);
		NSString *type = [pb availableTypeFromArray:[NSArray arrayWithObject:NSURLPboardType]];
		if (type != nil) {
			NSLog(@"Dragging Entered:");
			highlighted = YES;
			[self setNeedsDisplay:YES];
			return NSDragOperationCopy;
		}
	}
	return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender{
	highlighted = NO;
	[self setNeedsDisplay:YES];
}


- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
	return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender{
	NSPasteboard *pb = [sender draggingPasteboard];
	NSArray *pbArray = [pb propertyListForType:NSURLPboardType];
	NSString *urlstring = [pbArray objectAtIndex:0];
	NSURL *url = [NSURL URLWithString:urlstring];
	//there! Now we have the url. Let's download it.
	NSLog(@"Downloading : %@", url);
	
	//so somehow here I have to decide whether we're using a dictionary dropper or a image dropper....
	[photoSearchController downloadContentsOfURL:url toLocation:nil];
	return YES;
}



@end
